import { useRequest } from 'umi';
import { getCompanyID, getInvoiceCode, getPersonalInfo, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex, requestFunHtml } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { XQZF_STATE } from '../common/Format';
import { loadMdicIds } from '../services/DictionaryService';
import { getBaseUrl } from '@/services/urlconst';

const baseUrl = getBaseUrl();

export const NeedUpdateService = (): Record<string, any> => {
  /**添加需求 */
  const addNeedService = () => {
    const request = useRequest(
      (payload) => {
        const reqUrl = `${baseUrl}/purchaseNeedController/add`;
        let params = {
          ...payload,
          state: `${getTenantID()}${ILoadMapTypeAll.NEED_STATE}1000`,
          businessType: `100199031000`,
          createrID: getUserID(),
          createPositionID: getPosIDs(),
          createDepartmentID: getUserDepID(),
          createCompanyID: getCompanyID(),
          ownPhone: getPersonalInfo()?.phoneMobile,
          needCode: getInvoiceCode('XQ'),
        };
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );

    return {
      addNeedRun: request.run,
      addNeedLoading: request.loading,
    };
  };

  /**编辑需求 */
  const editNeedService = () => {
    const request = useRequest(
      (payload) => {
        const reqUrl = `${baseUrl}/purchaseNeedController/edit`;
        let params = {
          ...payload,
          modifyerID: getUserID(),
          modifyPositionID: getPosIDs(),
          modifyDepartmentID: getUserDepID(),
          modifyCompanyID: getCompanyID(),
        };
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );

    return {
      editNeedRun: request.run,
      editNeedLoading: request.loading,
    };
  };

  /**修改状态 */
  const modifyNeedStateService = () => {
    const request = useRequest(
      (payload) => {
        const reqUrl = `${baseUrl}/purchaseNeedController/upMeet`;
        return requestFunComponFiex('PUT', reqUrl, payload);
      },
      {
        manual: true,
      },
    );

    return {
      modifyNeedStateRun: request.run,
      modifyNeedStateLoading: request.loading,
    };
  };

  /**取消需求 */
  const cancelNeedService = () => {
    const request = useRequest(
      (payload) => {
        const reqUrl = `${baseUrl}/purchaseNeedController/cancel`;
        let params = {
          ...payload,
          state: XQZF_STATE(),
          cancelerID: getUserID(),
          cancelPositionID: getPosIDs(),
          cancelDepartmentID: getUserDepID(),
          cancelCompanyID: getCompanyID(),
        };
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );

    return {
      cancelNeedRun: request.run,
      cancelNeedLoading: request.loading,
    };
  };

  /**需求审核 */
  const auditNeedService = () => {
    const request = useRequest(
      (payload) => {
        const reqUrl = `${baseUrl}/purchaseNeedCheckController/audit`;
        let params = {
          ...payload,
          checkerID: getUserID(),
          checkPositionID: getPosIDs(),
          checkDepartmentID: getUserDepID(),
          checkCompanyID: getCompanyID(),
        };
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );

    return {
      auditNeedRun: request.run,
      auditNeedLoading: request.loading,
    };
  };

  /**查询需求详情 */
  const findNeedService = () => {
    const request = useRequest(
      (payload: any) => {
        /**
         * ======演示注释======
         * 【演示的时候用老接口】
         * */
        // let reqUrl = `${baseUrl}/purchaseNeedController/details/${payload.id}`; //【old】
        let reqUrl = `${baseUrl}/purchaseNeedController/getPurchaseNeedByID/${payload.id}/${getUserID()}`; //【new】
        return requestFunComponFiex('GET', reqUrl, {});
      },
      {
        manual: true,
      },
    );
    return {
      findNeedRun: request.run,
      findNeedLoading: request.loading,
    };
  };

  /**需求物料导出 */
  const exportNeedService = () => {
    const request = useRequest(
      (payload: any) => {
        let reqUrl = `${baseUrl}/purchaseNeedController/export`;
        return requestFunHtml('POST', reqUrl, { ...payload, dicTypeIds: loadMdicIds()?.join(',') });
      },
      {
        manual: true,
      },
    );
    return {
      exportNeedRun: request.run,
      exportNeedLoading: request.loading,
    };
  };

  /**审核*/
  const auditMaterialServices = (Callback: (res: Record<string, any>) => void) => {
    const request = useRequest(
      (payload) => {
        let bodyParams = {
          ...payload,
          checkerID: getUserID(),
          checkPositionID: getPosIDs(),
          checkDepartmentID: getUserDepID(),
          checkCompanyID: getCompanyID(),
        };

        let reqUrl = `${baseUrl}/purchaseNeedMaterialController/check`;
        return requestFunComponFiex('PUT', reqUrl, bodyParams);
      },
      {
        manual: true,
        onSuccess: Callback,
      },
    );

    return {
      auditMaterialRun: request?.run,
      auditMaterialLoading: request?.loading,
    };
  };

  return {
    addNeedService,
    findNeedService,
    editNeedService,
    auditNeedService,
    cancelNeedService,
    modifyNeedStateService,
    exportNeedService,
    auditMaterialServices,
  };
};

export const cancelServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      const reqUrl = `${baseUrl}/purchaseNeedController/cancel`;
      let params = {
        ...payload,
        state: XQZF_STATE(),
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    cancelRun: request?.run,
    cancelLoading: request?.loading,
  };
};

/**添加计划*/
export const addPlanServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      // const reqParams = setPlanDataFunc(payload)
      let reqParams = {
        ...payload,
        createrID: getUserID(), // 创建人信息
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        planCode: getInvoiceCode('JH'),
      };
      let reqUrl = `${baseUrl}/purchasePlanController/addPlan`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    addPlanRun: request?.run,
    addPlanLoading: request?.loading,
  };
};

/**同步PDF*/
export const syncPDFtoOAServices = (cb: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      let reqUrl = `${baseUrl}/purchaseNeedController/syncWorkFlow`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    syncPDFtoOARun: request?.run,
    syncPDFtoOALoading: request?.loading,
  };
};

/** 汇总计划接口 */
export const collectNeedPlanServices = (cb: (res: Record<string, any>) => void, requestUrl: string) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      let reqUrl = `${baseUrl}${requestUrl}`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );
  return {
    collectNeedPlanRun: request?.run,
    collectNeedPlanLoading: request?.loading,
  };
};
